package com.fancy.learncenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.fancy.learncenter.adapter.YgtPicAdapter;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.YgtGallaryDataBean;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.superservice.lya.R;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class YgtEditGalleryActivity extends BaseActivity {


    YgtPicAdapter picAdapter;

    @Bind(R.id.recycleView)
    RecyclerView recycleView;

    boolean isEditFlag = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ygt_edit_gallery, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);

        initToolbar("图集库");

        classifyId = getIntent().getStringExtra("classifyId");


        GridLayoutManager linearLayoutManager = new GridLayoutManager(this, 3);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);


        picAdapter = new YgtPicAdapter(this, new ArrayList());

        recycleView.setAdapter(picAdapter);

        setRight("编辑", new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (isEditFlag) {
                    isEditFlag = false;
                    setRight("编辑");

                    editLl.setVisibility(View.GONE);
                } else {
                    setRight("取消");

                    isEditFlag = true;
                    editLl.setVisibility(View.VISIBLE);
                }


                picAdapter.setEdit(isEditFlag);
                picAdapter.notifyDataSetChanged();


            }
        });


    }

    @Bind(R.id.edit_ll)
    LinearLayout editLl;

    @Override
    protected void onResume() {
        super.onResume();

        isEditFlag = false;
        setRight("编辑");
        editLl.setVisibility(View.GONE);

        getData();
    }

    String classifyId;

    private void getData() {
        isEditFlag = false;
        Map maps = new HashMap();

        maps.put("classifyId", classifyId);

        HttpMehtod.getInstance().ygtGallery(maps, new IdeaObserver<BaseDataBean<List<YgtGallaryDataBean>>>(this, true) {

            @Override
            public void onSuccess(BaseDataBean<List<YgtGallaryDataBean>> listBaseDataBean) {

                picAdapter.setEdit(isEditFlag);
                if (listBaseDataBean.getData() != null) {
                    picAdapter.notifyDataSetChanged((ArrayList<YgtGallaryDataBean>) listBaseDataBean.getData());
                }

//                viewPager.setResData(listBaseDataBean.getData(), R.layout.banner_item);
//                viewPager.setResData(listBaseDataBean.getData(), R.layout.banner_item);
            }

            @Override
            public void onFail(int errorCode) {

            }

        });

    }

    String galleryId = "";
    String galleryIds = "";

    private void deleteYgtGallery() {
        deletPosition = new ArrayList();

        Map maps = new HashMap();

        for (int i = 0; i < picAdapter.itemDatas.size(); i++) {
            if (picAdapter.itemDatas.get(i).getImg_status() == 1) {

                deletPosition.add(i);

                galleryId = galleryId + "," + picAdapter.itemDatas.get(i).getId();
                galleryIds = picAdapter.itemDatas.get(i).getId();
            }
        }

        if (TextUtils.isEmpty(galleryId)) {
            return;
        }

//        maps.put("galleryId", galleryId.substring(0, galleryId.length() - 2));
        maps.put("galleryId", galleryId.substring(1, galleryId.length()));
        maps.put("classifyId", classifyId);

        HttpMehtod.getInstance().deleteYgtGallery(maps, new IdeaObserver<BaseDataBean>(this, true) {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {

                ToastUtil.show("图集删除成功");

                int temp = 0;
                for (int i = 0; i < deletPosition.size(); i++) {
                    picAdapter.itemDatas.remove(deletPosition.get(i) - temp);
                    temp++;
                }

                picAdapter.notifyDataSetChanged();

            }

            @Override
            public void onFail(int errorCode) {

            }

        });

    }

    ArrayList<Integer> deletPosition;


    @OnClick({R.id.delete, R.id.add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.delete:
                deleteYgtGallery();
                break;
            case R.id.add:
                Intent intent = new Intent(this, YgtAddGallaryActivity.class);

                intent.putExtra("classifyId", classifyId);

                startActivity(intent);

                break;
        }
    }
}
